import React,{Component} from 'react';
import {captcha} from '../../api/account';
import {Button,message,Form } from 'antd';
class Code extends Component {
    constructor(props){
        super(props);
        this.state = {
            codeLogin:false,
            isCode:false,
            count:60
        }
    };
    getCode = async () => {
        const username = this.props.username;
        const module = this.props.type;
        if(!username){
            message.error("请输入邮箱");
            return;
        }
        this.setState({
            codeLogin:true,
        })
        let data = {
            username,
            module,
        }
        await captcha(data).then(res => {
            if(res.resCode === 0){
                message.success("验证码发送成功！");
                this.setState({
                    codeLogin:false
                });
                this.countDown();
            }
        })
    };
    //倒计时
    countDown = () => {
        let {count} = this.state;
        if(count === 1){
            this.setState({
                    count:60,
                    isCode:false,
            })
        }else{
            this.setState({
                count:count-1,
                isCode:true
            });
            setTimeout(() => this.countDown(),1000)
        }
    };

    render() {
        const {codeLogin,isCode,count} = this.state;
        return <Button type="primary" disabled={isCode} loading={codeLogin} danger blcok onClick={this.getCode}>{isCode?`${count}秒后重发`:'获取验证码'}</Button>
    }
}

export default Code;